package com.itheima.chapter08.collection;

import java.util.ArrayList;
import java.util.Iterator;

//迭代器
public class demo2 {
    public static void main(String[] args) {
        ArrayList<String> list =new ArrayList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
        list.add("dd");
        Iterator<String> it = list.iterator();
        boolean b = it.hasNext();
        System.out.println(b);
        String next = it.next();
        System.out.println(next);
        //遍历并删除指定元素
        while(it.hasNext()){
            String result = it.next();
            if ("bb".equals(result)){
                it.remove();//删除
            }
        }
        System.out.println(list);
        System.out.println("----------------------------");
        for (int i = 0; i < list.size(); i++) {
            String s =list.get(i);
            if ("cc".equals(s)){
                list.remove(i);
                i--;
            }
        }
        System.out.println(list);
        System.out.println("----------------------------");
        //遍历
        for (String s : list) {
            System.out.print(s +" ");
        }
    }
}
